package com.cqdev.washer.biz.service;

import com.cqdev.washer.biz.enums.ScriptExecuteResultEnum;
import com.cqdev.washer.biz.model.SqlScriptBO;
import com.cqdev.washer.infra.entity.SqlScript;
import com.cqdev.washer.infra.mapper.SqlScriptMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author yunfei.li
 *         类描述: TODO
 *         日期： 2018/7/2
 */
@Service
public class ScriptService {


    private final SqlScriptMapper sqlScriptMapper;

    public ScriptService(SqlScriptMapper sqlScriptMapper) {
        this.sqlScriptMapper = sqlScriptMapper;
    }

    public void deleteScript(Long id) {
        sqlScriptMapper.deleteByPrimaryKey(id);
    }

    public void updateScriptStatus(SqlScriptBO sqlScriptBO, ScriptExecuteResultEnum status) {
        SqlScript sqlScript = sqlScriptMapper.selectByPrimaryKey(sqlScriptBO.getId());
        if (sqlScript == null) {
            return;
        }
        int execute_count = sqlScript.getExecuteCount() == null ? 0 : sqlScript.getExecuteCount();
        sqlScript.setExecuteCount(execute_count + 1);
        sqlScript.setLastExecuteTime(new Date());
        sqlScript.setLastExecuteStatus(status.name());
        sqlScript.setLastElapsedTime(sqlScriptBO.getLastElapsedTime());

        sqlScriptMapper.updateByPrimaryKey(sqlScript);
    }

}
